package com.ehualu.getes.until;

import org.apache.catalina.User;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilders;
import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms;
import org.elasticsearch.search.aggregations.bucket.terms.Terms;
import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder;
import org.elasticsearch.search.aggregations.metrics.valuecount.ParsedValueCount;
import org.elasticsearch.search.aggregations.metrics.valuecount.ValueCountAggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.List;
import java.util.Map;

/**
 * @Author: 吴敬超
 * @Date: 2019/11/19 16:35
 */
public class ESfenz {


    private static String host = "37.158.97.130,37.158.97.131,37.158.97.132";
    private static int port = 24148;
    private static String schema = "http";

    public void teram(RestHighLevelClient client) {
        try {
            SearchRequest searchRequest = new SearchRequest("guoche_201911");

            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
            //			sourceBuilder.from(0); // 默认0
//            searchSourceBuilder.size(100);// 默认10条
            //根据姓名进行分组统计个数
            TermsAggregationBuilder field = AggregationBuilders.terms("terms_caddresscode").field("caddresscode.keyword");
            ValueCountAggregationBuilder countField = AggregationBuilders.count("count_caddresscode").field("caddresscode.keyword");
            field.subAggregation(countField);
            field.size(5);//todo

            searchSourceBuilder.aggregation(field);
            searchRequest.source(searchSourceBuilder);
            SearchResponse response = client.search(searchRequest);
            //分组在es中是分桶
            ParsedStringTerms termsName = response.getAggregations().get("terms_caddresscode");
            List<? extends Terms.Bucket> buckets = termsName.getBuckets();
            System.out.println(buckets.size());
            for (int i = 0; i < buckets.size(); i++) {

                String forkey = (String) buckets.get(i).getKey();
                System.out.println(forkey);
                ParsedValueCount countName = buckets.get(i).getAggregations().get("count_caddresscode");
                double value = countName.value();
                System.out.println(value);
            }


            System.out.println("************************************");
            buckets.forEach(naem -> {
                String key = (String) naem.getKey();
                System.out.println(key);
                ParsedValueCount countName = naem.getAggregations().get("count_caddresscode");
                double value = countName.value();
                System.out.println(value);
            });
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

//    public void teram(RestHighLevelClient client) {
//        try {
//            SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
//            //根据姓名进行分组统计个数
//            TermsAggregationBuilder field = AggregationBuilders.terms("terms_name").field("name");
//            ValueCountAggregationBuilder countField = AggregationBuilders.count("count_name").field("name");
//            field.subAggregation(countField);
//            searchSourceBuilder.aggregation(field);
//            SearchRequest searchRequest = new SearchRequest(User.USER_INDEX, User.USER_INDEX).source(searchSourceBuilder);
//            SearchResponse response = client.search(searchRequest);
//            //分组在es中是分桶
//            ParsedStringTerms termsName = response.getAggregations().get("terms_name");
//            List<? extends Terms.Bucket> buckets = termsName.getBuckets();
//            buckets.forEach(naem -> {
//                String key = (String) naem.getKey();
//                ParsedValueCount countName = naem.getAggregations().get("count_name");
//                double value = countName.value();
//                log.info("name , count {} {}", key, value);
//            });
//        } catch (IOException e) {
//            throw new RuntimeException(e);
//        }
//
//    }

    public static void main(String[] args) {

        ESClientbui esClientbui = new ESClientbui();
        RestHighLevelClient client = esClientbui.getHighLevelClient(host, port, schema);
        ESfenz eSfenz=new ESfenz();
        eSfenz.teram(client);


//
//        SearchRequest searchRequest = new SearchRequest("guoche_201911");
//        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        String dldmsz = "48072";
//        String reshstart = "2019-11-18T00:15:44.000Z";
//        String reshend = "2019-11-18T01:15:44.000Z";
//
//        QueryBuilder queryBuilder = QueryBuilders.boolQuery()
////                    termsQuery 函数用了变长变量 传多个 string可以 传数组也行
//                .must(QueryBuilders.termQuery("dldm", dldmsz))
////                    .must(QueryBuilders)
//                .must(QueryBuilders.rangeQuery("createtime").from(reshstart).to(reshend));
//
////        AggregationBuilder sumAge = AggregationBuilders.sum("total_sum").field("age");
////        sourceBuilder.aggregation(sumAge);
//
//
//        TermsAggregationBuilder field = AggregationBuilders.terms("terms_caddresscode").field("caddresscode.keyword");
//        ValueCountAggregationBuilder countField = AggregationBuilders.count("count_caddresscode").field("caddresscode.keyword");
//
//
//        field.subAggregation(countField);
//        field.size(Integer.MAX_VALUE);
//        sourceBuilder.aggregation(field);
//
//        sourceBuilder.query(queryBuilder);
//        searchRequest.source(sourceBuilder);
//        SearchResponse response = null;
//        try {
//            response = client.search(searchRequest);
//        } catch (IOException e) {
//            e.printStackTrace();
//        }
//        ParsedStringTerms termsName = response.getAggregations().get("terms_caddresscode");
//        List<? extends Terms.Bucket> buckets = termsName.getBuckets();
//        buckets.forEach(naem -> {
//            String key = (String) naem.getKey();
//
//            System.out.println(key);
//
//            ParsedValueCount countName = naem.getAggregations().get("count_caddresscode");
//            double value = countName.value();
////
//            System.out.println(value);
//
//
//        });


        esClientbui.close(client);
//
//        System.out.println("循环*****结束");


    }

}
